package com.example.smartcity_poverty.ui.AllService.JobSearch.activtiy

import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_js_me_add.*
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 * 修改个人简历
 */
class JsMeAddActivity : AppCompatActivity() {

    // 接收UserId
    private val id by lazy { intent.getStringExtra("userId")?.toInt() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_js_me_add)
        title = "个人简历"


        runThread()

        // 修改简历
        setJsMeAdd()
    }


    private fun runThread() = coroutine(Dispatchers.IO) {
        // 查询个人简历
        setUserAll()
    }

    /**
     * 查询个人简历
     */
    private suspend fun setUserAll() {
        api.loginAll(getToken()).apply {
            if (this.code == 200) {
                this.user?.let {
                    // 用户基本信息
                    jsMeUserTv1.setText("用户昵称: ${it.userName}")
                    jsMeUserTv2.setText("邮件: ${it.email}")
                    jsMeUserTv3.setText("电话号码: ${it.phonenumber}")
                    if (it.sex.toString() == "0") jsMeUserTv4.setText("性别: 男")
                    if (it.sex.toString() == "1") jsMeUserTv4.setText("性别: 女")
                }

            }
        }

        api.getJsMeAll(getToken(), id!!).apply {
            if (this.code == 200) {
                runOnUiThread {
                    this.data?.let {
                        // 求职信息
                        jsMeAddTv1.setText("${it.experience}")
                        jsMeAddTv2.setText("${it.mostEducation}")
                        jsMeAddTv3.setText("${it.address}")
                        jsMeAddTv4.setText("${it.positionId}")
                        jsMeAddTv5.setText("${it.money}")
                        jsMeAddTv6.setText("${it.education}")
                        jsMeAddTv7.setText("${it.individualResume}")
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }


    /**
     * 修改id
     */
    private fun setJsMeAdd() {
        jsMeAddOk.setOnClickListener {
            coroutine(Dispatchers.Main) {
                api.getJsMeAddAll(getToken(), JSONObject().apply {
                    put("id", 382)
                    put("mostEducation", jsMeAddTv2.text.toString())
                    put("education", jsMeAddTv6.text.toString())
                    put("address", jsMeAddTv3.text.toString())
                    put("experience", jsMeAddTv1.text.toString())
                    put("individualResume", jsMeAddTv7.text.toString())
                    put("money", jsMeAddTv5.text.toString())
                    put("positionId", "2")
                    put(
                        "files",
                        "https://file.100chui.com/files/upload/image/202105/d49648cd-d4e0-402d-915b-ac4a70098a59.jpg"
                    )

                }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                    runOnUiThread { this.msg.show() }
                }
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}